package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.greedao.entity.HotNotes;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class RecommendRecyclerViewAdapter extends RecyclerView.Adapter<RecommendRecyclerViewAdapter.RecommendViewHolder>{
    private List<HotNotes> hot_notes_datas;
    private ImageLoader imageLoader;
    private OnItemClickListener onItemClickListener;

    public RecommendRecyclerViewAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<HotNotes> datas) {
        hot_notes_datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecommendViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.recommend_recycler_view_item, null);
        return new RecommendViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final RecommendViewHolder holder, int position) {
        if (hot_notes_datas != null && hot_notes_datas.size() > 0) {
            if (hot_notes_datas.get(position) != null) {
                HotNotes hnd = hot_notes_datas.get(position);
                if (hnd.getTitle() != null && hnd.getTitle().length() > 0) {
                    holder.hotNotesTitleTv.setText(hnd.getTitle());
                    if (hnd.getUsername() != null && hnd.getUsername().length() > 0) {
                        holder.hotNotesUserTv.setText(hnd.getUsername());
                        if (hnd.getViews() != 0) {
                            holder.hotNotesViewsTv.setText(hnd.getViews() + "");
                            if (hnd.getReplys() != null) {
                                holder.hotNotesViewsCommentTv.setText(hnd.getReplys());
                                holder.hotNotesIv.setImageUrl(hnd.getPhoto(), imageLoader);
                            }
                        }
                    }
                }
            }
        }

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getAdapterPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, position);
                    return false;
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return hot_notes_datas != null && hot_notes_datas.size() > 0 ? hot_notes_datas.size() : 0;
    }

    class RecommendViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView hotNotesIv;
        private TextView hotNotesTitleTv, hotNotesUserTv, hotNotesViewsTv, hotNotesViewsCommentTv;

        public RecommendViewHolder(View v) {
            super(v);
            hotNotesIv = (NetworkImageView) v.findViewById(R.id.hotNotesIv);
            hotNotesTitleTv = (TextView) v.findViewById(R.id.hotNotesTitleTv);
            hotNotesUserTv = (TextView) v.findViewById(R.id.hotNotesUserTv);
            hotNotesViewsTv = (TextView) v.findViewById(R.id.hotNotesViewsTv);
            hotNotesViewsCommentTv = (TextView) v.findViewById(R.id.hotNotesViewsCommentTv);
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.onItemClickListener = mOnItemClickLitener;
    }




}
